#!/usr/bin/env python
#_*_coding:gbk_*_
# Copyright (c) 2008 crunch <hymm91@gmail.com>
# All rights reserved

#$Id: hbcws.py 35 2013-01-09 02:37:07Z hymm91@gmail.com $

import os, sys, time
import urllib
import socket
import httplib

httplib.socket.setdefaulttimeout(5.0)
 
class hbcws:
    def __init__(self, addr):
        self.addr = addr

    def get(self, info, dict="", mode="split", composite="", wild=0):           
        '''获取分词结果'''
        body = urllib.quote(info)
        headers = {'User-Agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)',
        'Content-type':'application/x-www-form-urlencoded',
        'Connection':'close',
        'Cache-Control':'no-cache'}
        conn = httplib.HTTPConnection(self.addr[0], self.addr[1])
        arg = list()
        if mode:
            arg.append('m=%s' % mode)
        if dict:
            arg.append('d=%s' % dict)
        if composite:
            arg.append('c=%s' % composite)
        if wild:
            arg.append('wild=%d' % wild)
        request = ''
        if arg:
            request = '/?%s' % '&'.join(arg)
            
        try:
            conn.request(method='POST', url=request, body=body, headers=headers)
        except socket.error, e:
            return (e[0], e)
        try:
            resp = conn.getresponse()
        except Exception, e:
            return (e[0], e)
        return (0, (resp.status, resp.read()))
 
if __name__ == '__main__':
    addr = ('192.168.3.234', 7777)
    ws = hbcws(addr)
    f = open(sys.argv[1], 'r')
    for line in f.readlines():
        line = line.strip()
        if not line:
            continue
            
        res, desc = ws.get(line)
        if res == 0 and desc:
            print desc[1].strip()
        else:
            print desc
        f.close()
#$Id: hbcws.py 35 2013-01-09 02:37:07Z hymm91@gmail.com $
